package com.example.demo.controllers;

import com.example.demo.entity.KH;
import com.example.demo.entity.YG;
import com.example.demo.services.KHService;
import com.example.demo.services.YGService;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RequestMapping("")
@Controller
public class LoginController {

    @Autowired
    private KHService khService;

    @Autowired
    private YGService ygService;

    @RequestMapping("/login")
    public String index(HttpServletRequest request, Model model){
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            KH users = khService.getUser(username, password);
            HttpSession session = request.getSession();
            session.setAttribute("user",users);
            request.getServletContext().setAttribute("userApplication",users);
            session.setMaxInactiveInterval(24*60*60);
            if (users != null) {
                return "user_info.html";
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("error", "用戶名或密碼輸入不正確");
        return "index.html";
    }

    @RequestMapping("/yg_login")
    public String login(HttpServletRequest request, Model model){
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            YG yg = ygService.getYG(username, password);
            ServletRequestAttributes req = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            HttpSession session = req.getRequest().getSession();
            session.setAttribute("userYg",yg);
            session.setMaxInactiveInterval(24*60*60);
            if (yg != null) {
//                request.getSession();
//                return "redirect:/demo/index";
//                return "user.html";
//                return "user_info.html";
//                return "pre_kdgl.html";
                return "kdgl.html";
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("error", "用戶名或密碼輸入不正確");
        return "login.html";
    }

    @RequestMapping("/register")
    public String register(){
        return "register.html";
    }

    @RequestMapping("/toLogin")
    public String register(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("userYg");
        return "login.html";
    }

    @RequestMapping("/toIndex")
    public String toIndex(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "index.html";
    }
}
